package com.yu.gulimall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.Query;
import com.yu.gulimall.product.dao.SkuInfoDao;
import com.yu.gulimall.product.entity.SkuInfoEntity;
import com.yu.gulimall.product.service.SkuInfoService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * {
     * page: 1,//当前页码
     * limit: 10,//每页记录数
     * sidx: 'id',//排序字段
     * order: 'asc/desc',//排序方式
     * key: '华为',//检索关键字
     * catelogId: 0,
     * brandId: 0,
     * min: 0,
     * max: 0
     * }
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils getSkuInfoList(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String catelogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (StrUtil.isNotBlank(key)) {
            skuInfoEntityQueryWrapper.and((item -> {
                item.eq("sku_id", key).or().like("sku_name", key);
            }));
        }
        if (StrUtil.isNotBlank(catelogId) && "0".equalsIgnoreCase(catelogId)) {
            skuInfoEntityQueryWrapper.eq("catalog_id", catelogId);
        }
        if (StrUtil.isNotBlank(brandId) && "0".equalsIgnoreCase(brandId)) {
            skuInfoEntityQueryWrapper.eq("brand_id", brandId);
        }
        if (StrUtil.isNotBlank(min)) {
            try {
                BigDecimal minMony = new BigDecimal(min);
                skuInfoEntityQueryWrapper.ge("price",minMony);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (StrUtil.isNotBlank(max)) {
            try {
                BigDecimal maxMony = new BigDecimal(max);
                skuInfoEntityQueryWrapper.le("price",maxMony);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {

        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }
}